// pages/familydoctor/sign/sign.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    attendSuccessImg: "",
    src: "",
    isshow: false,
    arr: [],
    data_arr: [],
  },
  // 点击照相



  takePictures: function() {
    var that = this;
    var uniacid = app.siteInfo.uniacid;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // console.log(res)
        //判断机型
        var model = "";
        wx.getSystemInfo({
          success: function(res) {
            var that = this;
            model = res.model;
          }
        })
        if (model.indexOf("iPhone") <= 0) {
          // that.uploadFileOpt(that.data.attendSuccessImg);
          that.setData({
            isshow: true
          })
        } else {
          drawCanvas();
        }
        // 缩放图片
        function drawCanvas() {
          const ctx = wx.createCanvasContext('attendCanvasId');
          ctx.drawImage(tempFilePaths[0], 0, 0, 94, 96);
          ctx.draw();
          that.prodImageOpt();
        }
        var tempFilePaths = res.tempFilePaths[0];
        wx.uploadFile({
          url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
          filePath: tempFilePaths,
          name: 'upfile',
          formData: {},
          success: function(res) {
            console.log(res.data)
            that.setData({
              attendSuccessImg: res.data
            })
          },
          fail: function(res) {
            console.log(res)
          },
        })
      }
    })
  },

  // 生成图片
  prodImageOpt: function() {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'attendCanvasId',
      success: function success(res) {
        that.setData({
          canvasImgUrl: res.tempFilePath
        });
        // 上传图片
        that.uploadFileOpt(that.data.canvasImgUrl);
      },
      complete: function complete(e) {}
    });
  },
  affirm: function() {
    var that = this
    var ff_id = that.data.ff_id
    if (that.data.attendSuccessImg == "") {
      wx.showToast({
        title: '请拍照',
        icon: "none"
      })
    } else {
      var attendSuccessImg = that.data.attendSuccessImg
      var t_id = that.data.t_id
      var sid = that.data.sid
      var key = that.data.key
      var name = that.data.name
      var j_id = that.data.j_id
      var names = that.data.names
      var money = that.data.money
      var teamname = that.data.teamname
      var hzsfz = that.data.hzsfz
      var ifhz = that.data.ifhz
      var zid = that.data.zid
      wx.navigateTo({
        url: '/hyb_yl/doctor/pages/familydoctor/signature/signature?ff_id=' + ff_id + '&qyimg=' + attendSuccessImg + '&t_id=' + t_id + '&sid=' + sid + '&key=' + key + '&name=' + name + '&j_id=' + j_id + '&names=' + names + '&money=' + money + '&teamname=' + teamname + '&hzsfz=' + hzsfz + '&ifhz=' + ifhz + '&zid=' + zid,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this
    var t_id = options.t_id
    var sid = options.sid
    var key = options.key
    var name = options.name
    var j_id = options.j_id
    var names = options.names
    var money = options.money
    var teamname = options.teamname
    var ifhz = options.ifhz
    var hzsfz = options.hzsfz
    var zid = options.zid
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data,
          t_id: t_id,
          sid: sid,
          key: key,
          name: name,
          j_id: j_id,
          names: names,
          money: money,
          teamname: teamname,
          ifhz: ifhz,
          hzsfz: hzsfz,
          zid: zid
        })
      },
    })
    if (that.data.attendSuccessImg != "") {
      that.setData({
        isshow: true
      })
    }

    if (options.item) {
      var arr1 = options.item
      console.log(arr1)
      that.setData({
        ff_id: arr1,
        bgc: bgc
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    if (this.data.attendSuccessImg === "") {
      this.setData({
        isshow: false
      })
    } else {
      this.setData({
        isshow: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})